<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--培优统计
class PeiyouTotal extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$examId = input("examId");
		$clazzType = input("clazzType");
		if(!$examId) return [];
		$where["考试ID"] = $examId;
		$where["实验班"] = $clazzType;
		$param = $this::queryScoreParam();
		$paramList = explode(",", $param);
		foreach ($paramList as $key => $value) {
			$paramList[$key] = "分段".($key+1)." as ".$value;
		}
		$fields = "班级,".implode(",", $paramList).",培优总分,培优名次";
		$result = Db::table("培优统计分段")
				  ->field($fields)
				  ->where($where)
				  ->order("班级")
				  ->select();
		return selectResult($result);
	}

	public function exportExcel() {
		$fields = input("fields");
		$examId = input("examId");
		$clazzType = input("clazzType");
		if(!$examId) return [];
		$where["考试ID"] = $examId;
		$where["实验班"] = $clazzType;
		$param = $this::queryScoreParam();
		$paramList = explode(",", $param);
		foreach ($paramList as $key => $value) {
			$paramList[$key] = "分段".($key+1)." as ".$value;
		}
		$fields = "班级,".implode(",", $paramList).",培优总分,培优名次";
		$result = Db::table("培优统计分段")
				  ->field($fields)
				  ->where($where)
				  ->order("班级")
				  ->select();
		$result = selectResult($result);
		//excel表头内容
		$param = "班级,".preg_replace("/\[|\]/","",$param).",培优总分,培优名次";
		$list = explode(",", $param);
		$header = [];
		foreach ($list as $key=> $value){
			$header[$value] = $value;
		}
		Excel::toExcel($result,'培优统计',$header);
		exit;
	}

	public function getFields(){
		$param = $this::queryScoreParam();
		$paramList = explode(",", $param);
		foreach ($paramList as $key => $value) {
			$paramList[$key] = preg_replace("/\[|\]/","",$value);
		}
		$fields = "班级,".implode(",", $paramList).",培优总分,培优名次";
	  	return $fields;
	}

	public function queryScoreParam() {
		$examId = input("examId");
		$clazzType = input("clazzType");
		$where["考试ID"] = $examId;
		$where["班级层次"] = $clazzType;
		$count = Db::table("培优总分公式")
				  ->where($where)
				  ->count();
		if($count == 0) {
			$where["考试ID"] = 0;	
		}
		$result = Db::table("培优总分公式")
				  ->field("培优分段 param")
				  ->where($where)
				  ->find();
		return $result["param"];
	}

}
